#!/bin/bash

# Slackware build script for liquidwar6

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20211031 bkw: BUILD=3
# - fix build on -current again.
# - use upstream's icon and .desktop.
# - install/remove info file in doinst/douninst.
# - new-style icons.
# - always include the extra game maps. they don't take up much space,
#   and it's one less hoop to make people jump through.

# 20170621 bkw: fix build on -current

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=liquidwar6
VERSION=${VERSION:-0.6.3902}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

MAPVER=${MAPVER:-0.0.13beta}
SHORTNAM=${PRGNAM/[0-9]*/}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-extra-maps-$MAPVER.tar.gz
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# -current needs this:
sed -i 's,-Werror,,' configure

# 20211031 bkw: it looks like we ought to be able to force the
# guile-config binary by setting GUILE_CONFIG in the env, but later
# on the configure script uses the hard-coded name "guile-config" (the
# one we want is "guile-config1.8"). So:
mkdir -p fakebin
ln -s /usr/bin/guile-config1.8 fakebin/guile-config
export PATH=$(pwd)/fakebin:$PATH

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --bindir=/usr/games \
  --datadir=/usr/share/games \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --infodir=/usr/info \
  --enable-shared \
  --disable-static \
  --enable-allinone \
  --mandir=/usr/man

make
make install-strip DESTDIR=$PKG

DIRVER="$( echo $VERSION | cut -d. -f1,2 )"
EXTRADIR=$PKG/usr/share/games/$PRGNAM-$DIRVER/map/extra
mkdir -p $EXTRADIR
cp -r $PRGNAM-extra-maps-$MAPVER/map/* $EXTRADIR

# the .desktop and icon should be installed in --datarootdir, not
# --datadir. Sigh.
mv $PKG/usr/share/games/applications $PKG/usr/share/applications
sed -i 's,Exec=,&/usr/games/,' $PKG/usr/share/applications/$PRGNAM.desktop
mv $PKG/usr/share/games/pixmaps $PKG/usr/share/pixmaps
rm -f $PKG/usr/share/pixmaps/$PRGNAM.png
# pixmaps still contains upstream's 48x48 xpm icon.

for px in 16 32 48 64 128; do
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  convert -resize $size data/icon/$PRGNAM.png $dir/$PRGNAM.png
done

gzip $PKG/usr/man/man6/$PRGNAM.6
( cd $PKG/usr/man/man6 ; ln -s $PRGNAM.6.gz $SHORTNAM.6.gz )
( cd $PKG/usr/games ; ln -s $PRGNAM $SHORTNAM )

# see doinst.sh and douninst.sh, for how to install/remove info files.
rm -f $PKG/usr/info/dir
gzip -9 $PKG/usr/info/*

# The package docs are just a skeleton, completely useless
# (the real docs are the info pages)
rm -rf $PKG/usr/share/doc

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README NEWS ChangeLog COPYING AUTHORS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed "s,@MAPVER@,$MAPVER," $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh  > $PKG/install/doinst.sh
cat $CWD/douninst.sh  > $PKG/install/douninst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
